package com.xhcl.web.controller.system;


import java.math.BigDecimal;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xhcl.common.annotation.Log;
import com.xhcl.common.enums.BusinessType;
import com.xhcl.system.domain.TGgDlyhwh;
import com.xhcl.system.domain.TSrhsCm;
import com.xhcl.system.domain.TSrhsDcqrmx;
import com.xhcl.system.domain.TSrhsJtmx;
import com.xhcl.system.service.ITGgDlyhwhService;
import com.xhcl.system.service.ITSrhsCmService;
import com.xhcl.system.service.ITSrhsDcqrmxService;
import com.xhcl.system.service.ITSrhsJtmxService;
import com.xhcl.common.core.controller.BaseController;
import com.xhcl.common.core.domain.AjaxResult;
import com.xhcl.framework.uitls.poi.ExcelUtil;
import com.xhcl.common.core.page.TableDataInfo;
import com.xhcl.common.core.text.Convert;
import com.xhcl.framework.shiro.util.ShiroUtils;

/**
 * 静态明细Controller
 * 
 * @author xhcl
 * @date 2020-09-06
 */
@Controller
@RequestMapping("/system/dcjtmx")
public class TSrhsDcJtmxController extends BaseController
{
    private String prefix = "system/dcjtmx";

    @Autowired
    private ITSrhsJtmxService tSrhsJtmxService;
    
    @Autowired
    private ITGgDlyhwhService tGgDlyhwhService;
    

    @Autowired
    private ITSrhsDcqrmxService tSrhsDcqrmxService;
    
    

    @RequiresPermissions("system:dcjtmx:view")
    @GetMapping()
    public String dcjtmx(ModelMap mmap)
    {
    	mmap.put("yhlist",tGgDlyhwhService.selectTGgDlyhwhList(null));
        return prefix + "/dcjtmx";
    }

    /**
     * 查询静态明细列表
     */
    @RequiresPermissions("system:dcjtmx:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSrhsJtmx tSrhsJtmx)
    {
        startPage();
        if (tSrhsJtmx.getParams().get("dfje")!=null && !tSrhsJtmx.getParams().get("dfje").equals("")) {
        	String[] dfjes=tSrhsJtmx.getParams().get("dfje").toString().split("\\,");
        	tSrhsJtmx.getParams().put("dfjes", dfjes);
        }
        List<TSrhsJtmx> list = tSrhsJtmxService.selectTSrhsDcJtmxList(tSrhsJtmx);
        return getDataTable(list);
    }

    /**
     * 导出静态明细列表
     */
    @RequiresPermissions("system:dcjtmx:export")
    @Log(title = "静态明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TSrhsJtmx tSrhsJtmx)
    {
        List<TSrhsJtmx> list = tSrhsJtmxService.selectTSrhsDcJtmxList(tSrhsJtmx);
        ExcelUtil<TSrhsJtmx> util = new ExcelUtil<TSrhsJtmx>(TSrhsJtmx.class);
        return util.exportExcel(list, "dcjtmx");
    }
  
    @GetMapping({"/add/{dclshs}/{dcje}"})
    public String add(@PathVariable("dclshs") String dclshs, @PathVariable("dcje") String dcje, ModelMap mmap)
    {
	      mmap.put("dclshs", dclshs);
	      if (dcje != null) {
	        BigDecimal zje = new BigDecimal(0);
	        for (int i = 0; i < dcje.split("\\,").length; i++) {
	          zje = zje.add(new BigDecimal(dcje.split("\\,")[i]));
	        }
	        mmap.put("zje", String.valueOf(zje));
	      }
	      String dclsh = dclshs.split("\\,")[0];
	      TSrhsJtmx srhsjtmx = new TSrhsJtmx();
	      srhsjtmx.setDclsh(dclsh);
	      List<TSrhsJtmx> list = tSrhsJtmxService.selectTSrhsDcJtmxList(srhsjtmx);
	      mmap.put("jkr", list.size() > 0 ? ((TSrhsJtmx)list.get(0)).getJkr() : "");
	      mmap.put("jkrzh", list.size() > 0 ? ((TSrhsJtmx)list.get(0)).getJkyhzh() : "");
	      mmap.put("jkryhmc", list.size() > 0 ? ((TSrhsJtmx)list.get(0)).getJkyhmc() : "");
	      mmap.put("yhbm", "");
	      mmap.put("skr", "");
	      mmap.put("skrzh", "");
	      mmap.put("skryhmc", "");
	      if (list.size() > 0) {
	        TGgDlyhwh dlyh = tGgDlyhwhService.selectTGgDlyhwhById(((TSrhsJtmx)list.get(0)).getYhbm());
	        if (dlyh != null) {
	          mmap.put("yhbm", dlyh.getYhbm());
	          mmap.put("skr", dlyh.getSkrmc());
	          mmap.put("skrzh", dlyh.getSkrzh());
	          mmap.put("skryhmc", dlyh.getYhmc());
	        }
	      }
	      return this.prefix + "/add";
    }
    
    
    
    @GetMapping({"/listdcqrmx/{qrlsh}"})
    public String listdcqrmx(@PathVariable("qrlsh") String qrlsh, ModelMap mmap)
    {
	      mmap.put("qrlsh", qrlsh);
	      return this.prefix + "/listdcqrmx";
    }
    
    /**
     * 查询单位待查确认明细查询列表
     */
    @RequiresPermissions("system:dcjtmx:list")
    @PostMapping("/listmx")
    @ResponseBody
    public TableDataInfo listmx(TSrhsDcqrmx tSrhsDcqrmx)
    {
        startPage();
        List<TSrhsDcqrmx> list = tSrhsDcqrmxService.selectTSrhsDcqrmxList(tSrhsDcqrmx);
        return getDataTable(list);
    }
    
    
    /**
     * 导出单位待查确认明细查询列表
     */
    @RequiresPermissions("system:dcjtmx:mxexport")
    @Log(title = "单位待查确认明细查询", businessType = BusinessType.EXPORT)
    @PostMapping("/mxexport")
    @ResponseBody
    public AjaxResult export(TSrhsDcqrmx tSrhsDcqrmx)
    {
        List<TSrhsDcqrmx> list = tSrhsDcqrmxService.selectTSrhsDcqrmxList(tSrhsDcqrmx);
        ExcelUtil<TSrhsDcqrmx> util = new ExcelUtil<TSrhsDcqrmx>(TSrhsDcqrmx.class);
        return util.exportExcel(list, "dcjtmx");
    }

    @RequiresPermissions({"system:dcjtmx:affirm"})
    @Log(title="待查确认", businessType=BusinessType.INSERT)
    @PostMapping({"/addsave"})
    @ResponseBody
    public AjaxResult addSave(@Validated TSrhsCm cm)
    {
      
      int i = tSrhsJtmxService.confirm(cm);
      return toAjax(i);
    }
    
    @RequiresPermissions("system:dcjtmx:autoaffirm")
    @Log(title = "自动确认", businessType = BusinessType.UPDATE)
    @PostMapping( "/autoaffirm")
    @ResponseBody
    public AjaxResult autoaffirm(String ids)
    {   
    	tSrhsJtmxService.affirm(Convert.toStrArray(ids));
        return AjaxResult.success();
    }
 
    @RequiresPermissions("system:dcjtmx:comfirm")
    @Log(title = "审核记账", businessType = BusinessType.UPDATE)
    @PostMapping( "/comfirm")
    @ResponseBody
    public AjaxResult comfirm(String ids)
    {   
    	tSrhsJtmxService.comfirm(Convert.toStrArray(ids),ShiroUtils.getLoginName());
        return AjaxResult.success();
    }
    
    @RequiresPermissions("system:dcjtmx:affirmreset")
    @Log(title = "待查冲销", businessType = BusinessType.UPDATE)
    @PostMapping( "/affirmreset")
    @ResponseBody
    public AjaxResult affirmreset(TSrhsJtmx tSrhsJtmx)
    {   
    	tSrhsJtmxService.affirmReset(String.valueOf(tSrhsJtmx.getPztxh()), tSrhsJtmx.getDclsh(), ShiroUtils.getLoginName(), tSrhsJtmx.getRemark());
        return AjaxResult.success();
    }
    @RequiresPermissions("system:dcjtmx:affirmcancel")
    @Log(title = "撤销确认", businessType = BusinessType.UPDATE)
    @PostMapping( "/affirmcancel")
    @ResponseBody
    public AjaxResult affirmCancel(String dclsh)
    {   
    	tSrhsJtmxService.affirmCancel(dclsh, ShiroUtils.getLoginName());
        return AjaxResult.success();
    }
    
    @RequiresPermissions("system:dcjtmx:confirmcancel")
    @Log(title = "已记账撤销确认", businessType = BusinessType.UPDATE)
    @PostMapping( "/confirmcancel")
    @ResponseBody
    public AjaxResult confirmcancel(String dclsh)
    {   
    	tSrhsJtmxService.verifyCancel(dclsh, ShiroUtils.getLoginName());
        return AjaxResult.success();
    }
    
}
